package com.itheima.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.entity.dto.VisitorDto;
import com.itheima.entity.pojo.User;
import com.itheima.entity.pojo.Visitor;
import com.itheima.entity.vo.VisitorVo;
import com.itheima.user.mapper.VisitorMapper;
import com.itheima.user.service.UserService;
import com.itheima.user.service.VisitorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author :wujunjie
 * createTime :2022-12-20 20:57
 * description :
 */
@Service
public class VisitorServiceImpl implements VisitorService {
    @Autowired
    private VisitorMapper visitorMapper;
    @Autowired
    private UserService userService;
    @Override
    public Boolean insertVisitor(VisitorDto visitorDto) {
        User user = userService.selectById(visitorDto.getUserId());
        if (user==null){
            throw new RuntimeException("被反问的用户不存在");
        }
        Visitor visitor = new Visitor();
        BeanUtils.copyProperties(visitorDto,visitor);
        visitor.setId(IdUtil.getSnowflakeNextIdStr());
        visitor.setVisitorId(ThreadLocalUtil.getId());
        visitor.setCreateTime(LocalDateTime.now());
        return visitorMapper.insertVisitor(visitor);
    }

    @Override
    public List<VisitorVo>  selectVisitor() {
        List<Visitor> visitors=visitorMapper.selectVisitorByUserId(ThreadLocalUtil.getId());
        if (visitors.size()==0){
            return null;
        }
        return BeanUtil.copyToList(visitors, VisitorVo.class);
    }

    @Override
    public Boolean deleteVisitor(String id) {
        Boolean delete=visitorMapper.deleteVisitorById(id);
        return delete;
    }
}
